//
//  QDMPNoticeTableViewCell.swift
//  QDMetroClient
//
//  Created by YueWen on 2019/4/12.
//  Copyright © 2019 YueWen. All rights reserved.
//

import UIKit

/// 跑马灯点击的回调
@objc protocol QDNoticeViewDelegate {
    /// 点击的回调
    @objc optional func noticeViewDidClick(view: QDNoticeView)
}


/// 紧急公告的视图
class QDNoticeView: UIView {
    
    /// 跑马灯标题
    @objc var title: String = ""{
        willSet {
            guard newValue != title else { return }
            if scrollable {
                marqueeView.textList = [newValue]; return
            }
            marqueeView.pause()
            titleLabel.text = newValue
        }
    }
    
    /// 跑马灯左侧边距
    @objc var imageViewMarginLeft: CGFloat = 20 {
        didSet {
            imageView.snp.updateConstraints { (make) in
                make.left.equalToSuperview().inset(imageViewMarginLeft)
            }
        }
    }
    
    /// 代理对象
    @objc weak var delegate: QDNoticeViewDelegate?
    
    /// 跑马灯视图
    @objc let marqueeView = MNMarqueeView()
    /// 左侧的文字
    @objc let imageView = UIImageView()
    /// 设置不需要滚动的标签
    @objc let titleLabel = UILabel()
    /// 是否需要滚动
    @objc var scrollable: Bool = true {
        willSet {
            titleLabel.isHidden = newValue
        }
    }
    
    private let actionControl = UIControl()
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        let backgroundColor = #colorLiteral(red: 1, green: 0.968627451, blue: 0.8901960784, alpha: 1)
        let textColor = #colorLiteral(red: 0.9882352941, green: 0.6235294118, blue: 0.03137254902, alpha: 1)
        
        self.backgroundColor = backgroundColor
        actionControl.backgroundColor = .clear
        imageView.image = "mp_notice_left".namedImage
        imageView.backgroundColor = backgroundColor
        
        marqueeView.backgroundColor = backgroundColor
        marqueeView.textColor = textColor
        marqueeView.fadeWidth = 8
        marqueeView.textFont = Font.regular.font(size: 14)
        marqueeView.textList = ["你的卡上能看到那是看你都卡你考虑呢"]
        
        titleLabel.backgroundColor = backgroundColor
        titleLabel.textColor = textColor
        titleLabel.font = Font.regular.font(size: 14)
        
        addSubview(imageView)
        addSubview(marqueeView)
        addSubview(titleLabel)
        addSubview(actionControl)
        
        imageView.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(20)
            make.bottom.top.equalToSuperview()
            make.width.equalTo(33)
        }
        
        marqueeView.snp.makeConstraints { (make) in
            make.left.equalTo(imageView.snp.right).offset(8)
            make.right.equalToSuperview().inset(20)
            make.centerY.equalToSuperview()
            make.height.equalTo(20)
        }
        
        
        titleLabel.snp.makeConstraints { (make) in
            make.edges.equalTo(marqueeView)
        }
        
        actionControl.snp.makeConstraints { (make) in
            make.edges.equalTo(marqueeView)
        }
        
        // 追加响应
        actionControl.addTarget(self, action: #selector(marqueeViewDidTap), for: .touchUpInside)
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    /// 跑马灯点击的方法
    @objc func marqueeViewDidTap(){
        delegate?.noticeViewDidClick?(view: self)
    }
}





/// 紧急公告的样式
final class QDMPNoticeTableViewCell: NWTableViewCell {
    
    /// 跑马灯设置公告内容
    var title: String? = "" {
        willSet {
            guard let value = newValue, value != title else { return }
            noticeView.title = newValue ?? ""
        }
    }
    /// 紧急公告的视图
    let noticeView = QDNoticeView()
    /// 阴影
    private let shadowView = UIImageView()

    override func buildView() {
        super.buildView()
        
        shadowView.image = "nav_shadow_bottom".namedImage

        contentView.addSubview(noticeView)
        contentView.addSubview(shadowView)

        noticeView.snp.makeConstraints { (make) in
            make.top.bottom.left.right.equalToSuperview()
        }
        
        shadowView.snp.makeConstraints { (make) in
            make.left.right.top.equalToSuperview()
            make.height.equalTo(9)
        }
    }
    
    
    func run(){
        if noticeView.titleLabel.isHidden {
            noticeView.marqueeView.run()
        }
    }
    
    func pause(){
        if noticeView.titleLabel.isHidden {
            noticeView.marqueeView.pause()
        }
    }
}
